# MODEL
from random import randint
from random import random


class Cells:

    def __init__(self, cells_list):
        self.cells = cells_list

    def all_move(self):
        for item in self.cells:
            item.move()


class Red_Cells(Cells):

    def add(self, x, y, pr_infect, red_colonia, blue_colonia, field):
        self.cells += [Red(x, y, pr_infect, red_colonia, blue_colonia, field)]

    def all_check_infect(self):
        for item in self.cells:
            item.check_infect()

    def del_cell(self, cell):
            del_obj = self.cells.index(cell)
            self.cells = self.cells[:del_obj] + self.cells[del_obj + 1:]


class Blue_Cells(Cells):

    def add(self, x, y, colonia, field):
        self.cells += [Blue(x, y, colonia, field)]
        
    def all_check_delete(self):
        for item in self.cells:
            item.check_delete()
            
    def del_cell(self, cell):
        del_obj = self.cells.index(cell)
        self.cells = self.cells[:del_obj] + self.cells[del_obj + 1:]    


class Green_Cells(Cells):

    def all_methos(self):
        new_cells = []
        for item in self.cells:
            new_cells += item.add()
        self.cells += new_cells

    def del_cell(self, cell):
        del_obj = self.cells.index(cell)
        self.cells = self.cells[:del_obj] + self.cells[del_obj + 1:]
        
class RGB:
    
    def move(self):
        distx = randint(-1, 1)
        disty = randint(-1, 1)
        
        if self.x + distx < self.height and self.x + distx >= 0:
            self.x += distx
        if self.y + disty < self.length and self.y + disty >= 0:
            self.y += disty        
        

class Green(RGB):
    def __init__(self, x, y, delay, colonia, field):
        self.x = x
        self.y = y
        self.delay = delay
        self.colonia = colonia
        self.field = field
        self.length = len(field[0])
        self.height = len(field)

    def add(self):
        return [Green(self.x, self.y, self.delay, self.colonia, self.field)]
        

class Red(RGB):

    def __init__(self, x, y, pr_infect, red_colonia, blue_colonia, field):
        self.x = x
        self.y = y
        self.pr_infect = pr_infect
        self.red_colonia = red_colonia
        self.blue_colonia = blue_colonia
        self.field = field
        self.length = len(field[0])
        self.height = len(field)

    def infect(self, cell):
        cell.colonia.del_cell(cell)
        if random() < self.pr_infect:
            self.red_colonia.add(self.x, self.y, self.pr_infect, self.red_colonia, self.blue_colonia, self.field)
        else:
            self.blue_colonia.add(self.x, self.y, self.blue_colonia, self.field)

    def check_infect(self):
        for item in self.field[self.x][self.y]:
            if str(type(item)) == "<class 'model.Green'>":
                gr = self.field[self.x][self.y].index(item)
                self.field[self.x][self.y] = self.field[self.x][self.y][:gr] + self.field[self.x][self.y][gr + 1:]
                self.infect(item)
        

class Blue(RGB):
    def __init__(self, x, y, colonia, field):
        self.x = x
        self.y = y
        self.colonia = colonia
        self.field = field
        self.length = len(field[0])
        self.height = len(field)        

    def check_delete(self):  # Delete red if red in this place
        for item in self.field[self.x][self.y]:
            if str(type(item)) == "<class 'model.Red'>":
                rc = self.field[self.x][self.y].index(item)
                self.field[self.x][self.y] = self.field[self.x][self.y][:rc] + self.field[self.x][self.y][rc + 1:]
                item.red_colonia.del_cell(item)

    def add(self):
        self.colonia.cells += [Blue(self.x, self.y, self.colonia, self.field)]


def make_field(length, height):  # Construct the field
    return [[[] for k in range(length)] for i in range(height)]
            



def fill_field(colonias_list, field):
    field2 = make_field(len(field[0]), len(field))

    for colonia in colonias_list:
        for cell in colonia.cells:
            field2[cell.x][cell.y] += [cell]
    for k in range(len(field2)):
        field[k] = field2[k]
            


#def main_loop(field):
    #k = 0
    #delay = green_colonia.delay
    #while True:
        #k += 1

        #'''Blue colonia => blue_colonia (Blue_Cells type)
           #Red colonia => red_colonia (Red_Cells type)
           #Green colonia => green_colonia (Green_Cells type)
           #Field => field
           #Length of field => length
           #Height of field => height'''

        #blue_colonia.all_check_delete()
        #red_colonia.all_check_infect()

        #if k % delay == 0:
            #green_colonia.all_methos()

        #blue_colonia.all_move()
        #red_colonia.all_move()
        #green_colonia.all_move()

        #field = make_field(length, height)
        #fill_field(colonias_list, field)
